package switchviz.graphic.signal;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

import org.jdom.Element;
import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.core.SwitchVizException;
import switchviz.core.alias.AliasDataModel;
import switchviz.core.alias.AliasFacade;
import switchviz.core.alias.AliasTree;
import switchviz.core.mib.MIBFacade;
import switchviz.gui.component.JColorPanel;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;



public class SimpleSignalPanel extends PaddingPanel implements ItemListener
{


	/**
	 * 
	 */
	private static final long serialVersionUID = 2130779159813407990L;
	private SimpleSignalPersistence _persistenceHandler = null;
	private SimpleSignalDataModel _datamodel = null;
	private JLabeldComponent _txtname = null;
	private JColorPanel _clsignal = null;
	private JLabel _lbltype = null;
	private JMaskedTextField _txtminvalue = null;
	private JMaskedTextField _txtmaxvalue = null;
	private JLabeldComponent _chbloweralarm = null;
	private JLabeldComponent _chbupperalarm = null;
	private JMaskedTextField _txtoffset = null;
	private JMaskedTextField _txtcoefficient = null;
	private JLabeldComponent _cmbaliases = null;
	private JMaskedTextField _txtportrequired = null; 
	private JLabeldComponent _chblogarithm = null;
	private JLabeldComponent _chbdifference = null;
	private JLabeldComponent _cmbpriority = null;
	
	private int portrequired = 0;
	
	public SimpleSignalPanel(Element node) 
	{
		_persistenceHandler = new SimpleSignalPersistence(node);
		initialize();
		this.setDataModel(_persistenceHandler.getDataModelFromElement());
	}
	
	private void initialize()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		_txtname = new JLabeldComponent("Signal Name: ", JLabeldComponent.TEXTFIELD);
		_clsignal = new JColorPanel("Signal Color: ");
		_lbltype = new JLabel();
		_chbdifference = new JLabeldComponent("Difference: ", JLabeldComponent.CHECKBOX);
		_chblogarithm = new JLabeldComponent("Logarithm: ", JLabeldComponent.CHECKBOX);
		_cmbaliases = new JLabeldComponent("Alias: ", JLabeldComponent.COMBOBOX);
		try {
			Iterator<AliasDataModel> aliasiter = AliasTree.getAliasList().iterator();
		
			while (aliasiter.hasNext())
			{
				_cmbaliases.getComboBox().addItem(aliasiter.next().getAlias());
			}
		
		} catch (JDOMException e) {
		} catch (IOException e) {}
		
		_cmbaliases.getComboBox().addItemListener(this);
		
		_cmbpriority = new JLabeldComponent("Detail: ", JLabeldComponent.COMBOBOX);
		_cmbpriority.getComboBox().addItem("Simple");
		_cmbpriority.getComboBox().addItem("Detailed");
		_cmbpriority.getComboBox().addItem("Off");
		
		_txtportrequired = new JMaskedTextField("Port: ", JMaskedTextField.INTNUM);
		_txtoffset = new JMaskedTextField("Offset: ", JMaskedTextField.FLOATNUM);
		_txtcoefficient = new JMaskedTextField("Coefficient: ", JMaskedTextField.FLOATNUM);
		_txtminvalue = new JMaskedTextField("Minvalue: ", JMaskedTextField.FLOATNUM);
		_txtmaxvalue = new JMaskedTextField("Maxvalue: ", JMaskedTextField.FLOATNUM);
		_chbloweralarm = new JLabeldComponent("Lower Alarm: ", JLabeldComponent.CHECKBOX);
		_chbupperalarm = new JLabeldComponent("Upper Alarm: ", JLabeldComponent.CHECKBOX);
		this.add(_lbltype);
		this.add(_txtname);
		this.add(_cmbpriority);
		this.add(_clsignal);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_cmbaliases);
		this.add(_txtportrequired);
		this.add(_chbdifference);
		this.add(_txtoffset);
		this.add(_txtcoefficient);
		this.add(_chblogarithm);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtminvalue);
		this.add(_txtmaxvalue);
		this.add(_chbloweralarm);
		this.add(_chbupperalarm);

	}
	
	public Element getElement()
	{
		getDataModel().setColor(_clsignal.getColor());
		getDataModel().setName(_txtname.getText());
		getDataModel().setMin(Float.parseFloat(_txtminvalue.getText()));
		getDataModel().setMax(Float.parseFloat(_txtmaxvalue.getText()));
		getDataModel().setOffset(Float.parseFloat(_txtoffset.getText()));
		getDataModel().setCoefficient(Float.parseFloat(_txtcoefficient.getText()));
		getDataModel().setAlias(_cmbaliases.getText());
		getDataModel().setDifference(_chbdifference.getCheckBox().isSelected());
		getDataModel().setLogarithm(_chblogarithm.getCheckBox().isSelected());
		
		int alarmmode = 0;
		
		if (_chbloweralarm.getCheckBox().isSelected())
			alarmmode += BasicSignalDataModel.ALARM_LOWER_BOUND;
		if (_chbupperalarm.getCheckBox().isSelected())
			alarmmode += BasicSignalDataModel.ALARM_UPPER_BOUND;
		
		if (_cmbpriority.getText().equals("Simple"))
			getDataModel().setPriority(0);
		else if (_cmbpriority.getText().equals("Detailed"))
			getDataModel().setPriority(1);
		else if (_cmbpriority.getText().equals("Off"))
			getDataModel().setPriority(2);
		
		getDataModel().setAlarmmode(alarmmode);
		getDataModel().setPortrequired(portrequired);
		getDataModel().setPort(Integer.parseInt(_txtportrequired.getText()));

		
		return _persistenceHandler.setDataModel(getDataModel());
	}
	
	private SimpleSignalDataModel getDataModel()
	{
		if(_datamodel == null)
		{
			_datamodel = new SimpleSignalDataModel();
			_datamodel.setType(BasicSignalDataModel.TYPESIMPLE);
		}
		return _datamodel;
	}

	private void setDataModel(SimpleSignalDataModel datamodel)
	{
		_datamodel = datamodel;
		_txtname.setText(_datamodel.getName());
		_clsignal.setColor(_datamodel.getColor());
		_lbltype.setText(_datamodel.getType());
		_txtminvalue.setText(String.valueOf(_datamodel.getMin()));
		_txtmaxvalue.setText(String.valueOf(_datamodel.getMax()));
		_txtoffset.setText(String.valueOf(_datamodel.getOffset()));
		_txtcoefficient.setText(String.valueOf(_datamodel.getCoefficient()));
		_cmbaliases.setText(_datamodel.getAlias());
		_txtportrequired.setText(String.valueOf(_datamodel.getPort()));
		portrequired = _datamodel.getPortrequired();
		_chbdifference.getCheckBox().setSelected(_datamodel.isDifference());
		_chblogarithm.getCheckBox().setSelected(_datamodel.isLogarithm());
		
		switch(_datamodel.getPortrequired())
		{
		case 0:
			_txtportrequired.setVisible(testPortRequired(_datamodel.getAlias()));
			break;
		case 1:
			_txtportrequired.setVisible(false);
			break;
		case 2:
			_txtportrequired.setVisible(true);
			break;
		}
		
		switch (getDataModel().getPriority())
		{
		case 0:
			_cmbpriority.setText("Simple");
			break;
		case 1:
			_cmbpriority.setText("Detailed");
			break;
		case 2:
			_cmbpriority.setText("Off");
			break;
		}
		
		switch (getDataModel().getAlarmmode())
		{
		case BasicSignalDataModel.ALARM_NO_BOUND:
			_chbloweralarm.getCheckBox().setSelected(false);
			_chbupperalarm.getCheckBox().setSelected(false);
			break;
		case BasicSignalDataModel.ALARM_LOWER_BOUND:
			_chbloweralarm.getCheckBox().setSelected(true);
			_chbupperalarm.getCheckBox().setSelected(false);
			break;
		case BasicSignalDataModel.ALARM_UPPER_BOUND:
			_chbloweralarm.getCheckBox().setSelected(false);
			_chbupperalarm.getCheckBox().setSelected(true);
			break;
		case BasicSignalDataModel.ALARM_BOTH_BOUNDS:
			_chbloweralarm.getCheckBox().setSelected(true);
			_chbupperalarm.getCheckBox().setSelected(true);
			break;
		}
	}
	
	private boolean testPortRequired(String alias)
	{
		boolean required = false;

		try {
			required = MIBFacade.isPortRequired(AliasFacade.getOid(alias));

			if (required)
				portrequired=2;
			else
				portrequired=1;
		} catch (SwitchVizException e) {
			portrequired=0;
		}
		
		return required;
	}

	public void itemStateChanged(ItemEvent e) 
	{
		_txtportrequired.setVisible(testPortRequired(_cmbaliases.getText()));
	}
}
